package cn.dakaqi.group.services;

import cn.dakaqi.api.qnzyz.member.BHMemberService;
import cn.dakaqi.api.qnzyz.pojo.BHMember;
import cn.dakaqi.entities.Group;
import cn.dakaqi.entities.GroupVolunteer;
import cn.dakaqi.entities.Volunteer;
import cn.dakaqi.member.service.VolunteerService;
import cn.dakaqi.utils.BaseDAO;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.ErrorCodeMsg;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import cn.dakaqi.utils.rongYun.PushGroupMesssage;
import cn.dakaqi.utils.rongYun.PushSystemMesssage;
import cn.dakaqi.volunteer.tag.PlatformGroupVolunteerService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/4/16.
 */
@Component(value = "groupVolunteerService")
@Transactional
public class GroupVolunteerServiceImpl implements GroupVolunteerService
{
    @Autowired
    GroupVolunteerDao groupVolunteerDao;
    @Autowired
    GroupService groupService;
    @Autowired
    VolunteerService volunteerService;
    @Autowired
    PlatformGroupVolunteerService platformGroupVolunteerService;
    @Autowired
    BaseDAO baseDAO;
    @Autowired
    BHMemberService bhMemberService;

    @Override
    public void save(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);
        String memberCode = jsonObject.getString("memberCode");    //志愿者
        String groupCode = jsonObject.getString("groupCode");           //社团

        GroupVolunteer groupVolunteer = new GroupVolunteer();
        Group group = groupService.findByGroupCode(groupCode);
        Volunteer volunteer = volunteerService.findByMemberCode(memberCode);

        if(Volunteer.checkInfo(volunteer) == false)
            throw new ServiceRuntimeException(ErrorCodeMsg.USER_INFO_NOT_FULL);

        groupVolunteer.setVolunteer(volunteer);
        groupVolunteer.setGroup(group);
        groupVolunteer.setCreateTime(Clock.DEFAULT.getCurrentDate());
        groupVolunteer.setRole(DKQConstant.ROLES_VOLUNTEER);
        groupVolunteer.setStatus(DKQConstant.APPLY_STATUS);
        save(groupVolunteer);
    }

    @Override
    public void save(GroupVolunteer groupVolunteer) throws ServiceRuntimeException
    {
        //检查当前用户是否已加入本社团
        GroupVolunteer temp = this.findByMemberAndGroup(groupVolunteer.getVolunteer().getMemberCode(),DKQConstant.APPLY_STATUS_ALL , groupVolunteer.getGroup().getGroupCode());
        if(null != temp)
        {
            if(temp.getStatus() == DKQConstant.APPLY_STATUS_OK)
                throw new ServiceRuntimeException("您已是本社团成员");
            else if(temp.getStatus() == DKQConstant.APPLY_STATUS)
                throw new ServiceRuntimeException("请耐心等待管理员审核");
            else if(temp.getStatus() == DKQConstant.APPLY_STATUS_REFUST || temp.getStatus() == DKQConstant.APPLY_STATUS_EXPEL ||temp.getStatus() == DKQConstant.APPLY_STATUS_EXIT)
            {
                temp.setStatus(DKQConstant.APPLY_STATUS);
                this.groupVolunteerDao.save(temp);
                PushGroupMesssage.getInstance().applyGroupVolunteer(temp.getGroup().getName(),temp.getVolunteer().getMemberCode());
                PushGroupMesssage.getInstance().waiteVerfierGroupVolunteer(baseDAO,temp.getGroup().getId(),temp.getGroup().getName(),temp.getVolunteer().getRealName(),temp.getVolunteer().getMemberCode(),temp.getId());
            }
        }
        else
        {
            this.groupVolunteerDao.save(groupVolunteer);
            //更新社团成员数量
            Group group = groupVolunteer.getGroup();
            if(groupVolunteer.getStatus() == DKQConstant.APPLY_STATUS_OK)
            {
                group.setVolunteers(group.getVolunteers() + 1);
                this.groupService.update(group);
                joinGroupMesssage(groupVolunteer.getVolunteer().getMemberCode(),groupVolunteer.getVolunteer().getNickName(),groupVolunteer.getGroup().getGroupCode(),groupVolunteer.getGroup().getShortName());
                PushGroupMesssage.getInstance().agreeGroupVolunteer(group.getName(),groupVolunteer.getVolunteer().getMemberCode());
            }
            else
            {
                PushGroupMesssage.getInstance().applyGroupVolunteer(groupVolunteer.getGroup().getName(),groupVolunteer.getVolunteer().getMemberCode());
                PushGroupMesssage.getInstance().waiteVerfierGroupVolunteer(baseDAO, group.getId(), group.getName(), groupVolunteer.getVolunteer().getRealName(), groupVolunteer.getVolunteer().getMemberCode(), groupVolunteer.getId());
            }
        }

    }

    private void joinGroupMesssage(String userId,String userName,String groupCode,String groupName)
    {
        PushGroupMesssage.getInstance().joinGroup("G",userId,userName, groupCode, groupName);
    }
    private void quitGroupMesssage(String userId,String groupCode)
    {
        PushGroupMesssage.getInstance().quitGroup(userId, groupCode);
    }

    @Override
    public void importData(GroupVolunteer groupVolunteer) throws ServiceRuntimeException
    {
        try
        {
            //检查当前用户是否已加入本社团
            GroupVolunteer temp = this.findByMemberAndGroup(groupVolunteer.getVolunteer().getMemberCode(),DKQConstant.APPLY_STATUS_ALL , groupVolunteer.getGroup().getGroupCode());
            if(null == temp)
            {
                this.groupVolunteerDao.save(groupVolunteer);
                Group group = groupVolunteer.getGroup();
                if(groupVolunteer.getStatus() == DKQConstant.APPLY_STATUS_OK)
                {
                    //更新社团成员数量
                    group.setVolunteers(group.getVolunteers() + 1);
                    this.groupService.update(group);
                    submit2BHVolunteer(groupVolunteer);
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 将当前用户注册至北航获取志愿者一号通
     * @param groupVolunteer
     */
    private void submit2BHVolunteer(GroupVolunteer groupVolunteer)throws Exception
    {
        try
        {
            Volunteer volunteer = groupVolunteer.getVolunteer();

            if(null == volunteer.getVolunteerCode() || StringUtils.isBlank(volunteer.getVolunteerCode()) || "null".equals(volunteer.getVolunteerCode().toLowerCase()))
            {
                String voluteerCode = bhMemberService.simpleReg(BHMember.toBHMember(groupVolunteer.getVolunteer(),groupVolunteer.getGroup().getGroupCode()));
                if(StringUtils.isEmpty(voluteerCode))
                    throw new ServiceRuntimeException("团中央志愿者注册失败");
                volunteer.setVolunteerCode(voluteerCode);
                volunteer.setLocked(DKQConstant.MEMBER_LOCK_1);

                PushSystemMesssage.getInstance().agreenVolunteerApply(voluteerCode,groupVolunteer.getVolunteer().getMemberCode());
//                //推送消息至志愿者用户
//                List<String> volunteers = new ArrayList<String>();
//                volunteers.add(groupVolunteer.getVolunteer().getMemberCode());
//                PushMessageUtil.publishSystemMessage("您已经是打卡器的认证用户，一号通为:"+voluteerCode,null,PushMessageUtil.SYSTEM_HELPER ,volunteers);

                this.volunteerService.updateVolunteer(volunteer);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void del(Long id) throws ServiceRuntimeException
    {
        GroupVolunteer groupVolunteer = this.findOne(id);
        if(null != groupVolunteer)
        {
            groupVolunteer.setDelStatus(DKQConstant.DEL_YES);
            Group group = groupVolunteer.getGroup();
            int count = group.getVolunteers()-1;
            if(count<=0)
                count = 0;
            group.setVolunteers(count);
            this.groupService.update(group);
            this.update(groupVolunteer);

            quitGroupMesssage(groupVolunteer.getVolunteer().getMemberCode(), groupVolunteer.getGroup().getGroupCode());
        }
    }

    @Override
    public void update(GroupVolunteer groupVolunteer) throws ServiceRuntimeException
    {
        this.groupVolunteerDao.save(groupVolunteer);
    }

    @Override
    public void setAdmin(String jsonData) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Long groupVolunteerId = jsonObject.getLong("groupVolunteerId");
        String memberCode = jsonObject.getString("memberCode");
        int status = jsonObject.getInteger("status");
        this.setAdmin(groupVolunteerId,status,memberCode);
    }

    @Override
    public void setAdmin(Long id, int role,String memberCode) throws ServiceRuntimeException
    {
        Volunteer volunteer = this.volunteerService.findByMemberCode(memberCode);
        GroupVolunteer groupVolunteer = findOne(id);
        if(null != groupVolunteer && groupVolunteer.getRole() != DKQConstant.ROLES_CREATEER)
        {
            groupVolunteer.setRole(role);
            this.update(groupVolunteer);
            if(role == DKQConstant.ROLES_ADMIN)
                PushGroupMesssage.getInstance().setGroupAdmin(groupVolunteer.getGroup().getName(),groupVolunteer.getVolunteer().getMemberCode());
            else if(role == DKQConstant.ROLES_VOLUNTEER)
                PushGroupMesssage.getInstance().cancelGroupAdmin(groupVolunteer.getGroup().getName(), volunteer.getRealName(),groupVolunteer.getVolunteer().getMemberCode());
        }
        else
            throw new ServiceRuntimeException("创建人不可以取消管理员权限");
    }

    @Override
    public void setDemo(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);
        Long groupVolunteerId = jsonObject.getLong("groupVolunteerId");
        String memberCode = jsonObject.getString("memberCode");
        String demo = jsonObject.getString("demo");
        this.setDemo(groupVolunteerId, demo);
    }

    @Override
    public void setDemo(Long groupVolunteerId, String demo) throws ServiceRuntimeException
    {
        GroupVolunteer groupVolunteer = findOne(groupVolunteerId);
        if(null != groupVolunteer)
        {
            groupVolunteer.setDemo(demo);
            this.update(groupVolunteer);
        }
    }

    @Override
    public void expel(String param)
    {
        JSONObject jsonObject = JSON.parseObject(param);
        Long groupVolunteerId = jsonObject.getLong("groupVolunteerId");
        String memberCode = jsonObject.getString("memberCode");
        this.expel(groupVolunteerId);
    }

    @Override
    public void expel(Long groupVolunteerId) throws ServiceRuntimeException
    {
        GroupVolunteer groupVolunteer = findOne(groupVolunteerId);
        if(null != groupVolunteer)
        {
            if(groupVolunteer.getRole() == DKQConstant.ROLES_CREATEER)
                throw new ServiceRuntimeException("社团创建者不可以开除");

            groupVolunteer.setStatus(DKQConstant.APPLY_STATUS_EXPEL);
            this.update(groupVolunteer);

            Group group = groupVolunteer.getGroup();
            int count = group.getVolunteers()-1;
            if(count<=0)
                count = 0;
            group.setVolunteers(count);
            this.groupService.update(group);
            //将当前用户加入当前社团所挂靠平台
            platformGroupVolunteerService.expelGroup(groupVolunteer.getGroup().getId());
            quitGroupMesssage(groupVolunteer.getVolunteer().getMemberCode(), groupVolunteer.getGroup().getGroupCode());
            PushSystemMesssage.getInstance().expelGroupVolunteer(groupVolunteer.getGroup().getName(),groupVolunteer.getVolunteer().getMemberCode());
        }
    }

    @Override
    public GroupVolunteer findOne(Long id) throws ServiceRuntimeException
    {
        return this.groupVolunteerDao.findOne(id);
    }

    @Override
    public Page<GroupVolunteer> queryMemberCode(String memberCode, int status, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String,Object> map = new HashMap<String, Object>();
        //map.put("LIKE_NAME",name);
        //Map<String, SearchFilter> filters = SearchFilter.parse(map);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec, pageRequest);
    }

    @Override
    public Page<GroupVolunteer> queryGroupCode(String groupCode, int status,int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String,Object> map = new HashMap<String, Object>();
        //map.put("LIKE_NAME",name);
        //Map<String, SearchFilter> filters = SearchFilter.parse(map);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec, pageRequest);
    }

    @Override
    public List<GroupVolunteer> queryGroupCode(String groupCode) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec);
    }

    @Override
    public GroupVolunteer findMemberLast(String memberCode) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        List<GroupVolunteer> list =  this.groupVolunteerDao.findAll(spec);
        if(null != list && list.size()>0)
        {
            return list.get(list.size()-1);
        }
        return null;
    }

    @Override
    public GroupVolunteer findByMemberAndGroup(String memberCode,int status, String groupCode) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        if(DKQConstant.APPLY_STATUS_ALL != status)
            filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));

        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        List<GroupVolunteer> list =  this.groupVolunteerDao.findAll(spec);
        if(null != list && list.size()>0)
        {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Page<GroupVolunteer> findByVolunteerName(String groupCode, String name, int pageNumber)
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 50, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.nickName", new SearchFilter("volunteer.nickName", SearchFilter.Operator.LIKE, name));
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec,pageRequest);
    }

    @Override
    public Page<GroupVolunteer> findByGroup(String groupCode, int pageNumber,int unLock) throws ServiceRuntimeException
    {

        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.locked", new SearchFilter("volunteer.locked", SearchFilter.Operator.EQ, unLock));
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec,pageRequest);
    }

    @Override
    public void updateStatus(String jsonData) throws ServiceRuntimeException
    {
        try
        {
            JSONObject jsonObject = JSON.parseObject(jsonData);
            Long groupVolunteerId = jsonObject.getLong("groupVolunteerId");
            String memberCode = jsonObject.getString("memberCode");
            String refuseCase = jsonObject.getString("refuseCase");
            int status = jsonObject.getInteger("status");
            updateStatus(groupVolunteerId,status,memberCode,refuseCase);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void updateStatus(Long id, int status,String memberCode,String refuseCase) throws ServiceRuntimeException
    {
        try
        {
            GroupVolunteer groupVolunteer = findOne(id);
            if(null != groupVolunteer)
            {
                if(status == DKQConstant.APPLY_STATUS_OK)
                {

                    //将当前用户注册至北航
                    if(StringUtils.isBlank(groupVolunteer.getVolunteer().getVolunteerCode()))
                        submit2BHVolunteer(groupVolunteer);

                    groupVolunteer.setStatus(status);
                    this.update(groupVolunteer);
                    //将当前用户加入当前社团所挂靠平台
                    platformGroupVolunteerService.save(groupVolunteer.getGroup().getId(),groupVolunteer.getVolunteer().getId());
                    joinGroupMesssage(groupVolunteer.getVolunteer().getMemberCode(),groupVolunteer.getVolunteer().getNickName(),groupVolunteer.getGroup().getGroupCode(),groupVolunteer.getGroup().getShortName());
                    PushGroupMesssage.getInstance().agreeGroupVolunteer(groupVolunteer.getGroup().getName(),groupVolunteer.getVolunteer().getMemberCode());
                }
                else if(status == DKQConstant.APPLY_STATUS_REFUST)
                {
                    groupVolunteer.setStatus(status);
                    groupVolunteer.setRefuseCase(groupVolunteer.getRefuseCase()+";"+refuseCase);
                    PushSystemMesssage.getInstance().refuseGroupVolunteer(groupVolunteer.getGroup().getName(),groupVolunteer.getVolunteer().getMemberCode());
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
            try
            {
                throw e;
            } catch (Exception e1)
            {
                e1.printStackTrace();
            }
        }

    }

    @Override
    public int counts(Long groupId,int status) throws ServiceRuntimeException
    {
        return groupVolunteerDao.counts(groupId,status);
    }

    @Override
    public List<GroupVolunteer> findByAdmin(String memberCode) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        filters.put("role", new SearchFilter("role", SearchFilter.Operator.GT, DKQConstant.ROLES_VOLUNTEER));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec);
    }

    @Override
    public List<GroupVolunteer> findAdminByGroup(String groupCode) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("role", new SearchFilter("role", SearchFilter.Operator.GT, DKQConstant.ROLES_VOLUNTEER));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec);
    }

    @Override
    public void exit(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);
        String memberCode = jsonObject.getString("memberCode");    //志愿者
        String groupCode = jsonObject.getString("groupCode");           //社团

        GroupVolunteer groupVolunteer = findByMemberAndGroup(memberCode,DKQConstant.APPLY_STATUS_ALL, groupCode);
        if(null != groupVolunteer)
        {
            exit(groupVolunteer);
        }
    }

    @Override
    public void exit(GroupVolunteer groupVolunteer) throws ServiceRuntimeException
    {
        if(groupVolunteer.getRole() == DKQConstant.ROLES_CREATEER)
            throw new ServiceRuntimeException("创始人不可以退出");

        if(null == groupVolunteer)
            return;
        //更新社团成员数量
        Group group = groupVolunteer.getGroup();
        int count = group.getVolunteers()-1;
        if(count<=0)
            count = 0;
        group.setVolunteers(count);
        this.groupService.update(group);
        //
        groupVolunteer.setStatus(DKQConstant.APPLY_STATUS_EXIT);
        this.groupVolunteerDao.save(groupVolunteer);

        quitGroupMesssage(groupVolunteer.getVolunteer().getMemberCode(),groupVolunteer.getGroup().getGroupCode());
        //将当前用户加入当前社团所挂靠平台
        platformGroupVolunteerService.exitGroup(groupVolunteer.getGroup().getId());

    }

    @Override
    public Page<GroupVolunteer> findNameLike(String groupCode, String realName) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(0, 50, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("volunteer.realName", new SearchFilter("volunteer.realName", SearchFilter.Operator.LIKE, realName));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec,pageRequest);
    }

    @Override
    public Page<GroupVolunteer> findByJob(String groupCode, String job) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(0, 50, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("volunteer.job", new SearchFilter("volunteer.job", SearchFilter.Operator.LIKE, job));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec,pageRequest);
    }

    @Override
    public Page<GroupVolunteer> findBySkill(String groupCode, String skill) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(0, 50, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("volunteer.skill", new SearchFilter("volunteer.skill", SearchFilter.Operator.LIKE, skill));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec,pageRequest);
    }

    @Override
    public Page<GroupVolunteer> findByServiceField(String groupCode, String serviceField) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(0, 50, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("volunteer.serviceField", new SearchFilter("volunteer.serviceField", SearchFilter.Operator.LIKE, serviceField));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        return this.groupVolunteerDao.findAll(spec,pageRequest);
    }

    @Override
    public GroupVolunteer searchMonitor(String groupCode, String realName) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("group.groupCode", new SearchFilter("group.groupCode", SearchFilter.Operator.EQ, groupCode));
        filters.put("volunteer.realName", new SearchFilter("volunteer.realName", SearchFilter.Operator.EQ, realName));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS_OK));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupVolunteer.class);
        List<GroupVolunteer> list = this.groupVolunteerDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public void save(Volunteer volunteer, String groupCode) throws ServiceRuntimeException
    {
        GroupVolunteer groupVolunteer = new GroupVolunteer();
        groupVolunteer.setVolunteer(volunteer);
        Group group = this.groupService.findByGroupCode(groupCode);
        groupVolunteer.setGroup(group);
        groupVolunteer.setRole(DKQConstant.ROLES_VOLUNTEER);
        groupVolunteer.setStatus(DKQConstant.APPLY_STATUS_OK);
        groupVolunteer.setCreateTime(org.springside.modules.utils.Clock.DEFAULT.getCurrentDate());
        groupVolunteer.setDelStatus(DKQConstant.DEL_NO);
        this.save(groupVolunteer);
    }
}
